package com.itheima.consumer.mq;

import com.itheima.consumer.DO.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * @Author wyh
 * @Date 2025/5/8 18:48
 * @description:
 */
@Component
@Slf4j
public class SpringRabbitListenner {
    @RabbitListener(queues = "simple.q1")
    public void listen(String msg){
        log.info("消费者收到消息：{}",msg);
    }

    @RabbitListener(queues = "work.q1")
    public void listen1(String msg) throws InterruptedException {
        log.info("消费者1收到消息：{}",msg);
        Thread.sleep(20);
    }
    @RabbitListener(queues = "work.q1")
    public void listen2(String msg) throws InterruptedException {
        System.out.println("2收到消息："+msg);
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg){
        log.info("消费者1监听到fanout.queue1消息接收【{}】",msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg){
        log.info("消费者2监听到fanout.queue2消息接收【{}】",msg);
    }

/*    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg){
        log.info("消费者1 direct监听到direct.queue1消息接收【{}】",msg);
    }
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg){
        log.info("消费者2 direct监听到direct.queue2消息接收【{}】",msg);
    }*/

    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg){
        log.info("消费者1 direct监听到topic.queue1消息接收【{}】",msg);
    }
    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg){
        log.info("消费者2 direct监听到topic.queue2消息接收【{}】",msg);
    }



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1",durable = "ture" ,arguments = @Argument(name = "x-queue-mode",value = "lazy")),
            exchange = @Exchange(name = "itwyh.exchange",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg){
        log.info("消费者1 direct监听到direct.queue1消息接收【{}】",msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itwyh.exchange",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg){
        log.info("消费者2 direct监听到direct.queue2消息接收【{}】",msg);
    }
    @RabbitListener(queues = "object.queue")
    public void listenObject(User user){
        log.info("消费者收到消息：{}",user);
    }


    // 延迟消息
    // 死信机 + TTL实现延迟队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dlx.queue", durable = "true"),
            exchange = @Exchange(name = "dlx.direct"),
            key = "hi"
    ))
    public void listenDlxMessage(String msg){
        log.info("接收到dlx.queue的延迟消息：{}", msg);
    }

    //Delay Exchange插件实现延迟队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "delay"
    ))
    public void listenDelayMessage(String msg){
        log.info("接收到delay.queue的延迟消息：{}", msg);
    }

    //lazy queue
    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode", value = "lazy")
    ))
    public void listenLazyQueue(String msg){
        log.info("接收到 lazy.queue的消息：{}", msg);
    }

}
